import Vue from 'vue'
import Router from 'vue-router'
// import templateFile from '@/components/templateFile' // 模板页
// import test from '@/views/test'
import {Message} from 'element-ui';

import login from '@/views/login/loginLogic' // 登录
import navigationPage from '@/views/navigationPage/navigationPageLogic';
import frame from '@/views/frame/frameView'
import notFound from '@/views/404.vue';


let session = window.sessionStorage;

Vue.use(Router);

const router = new Router({
  // mode: 'history',
  routes: [
    {
      path: '/',
      name: 'login',
      component: login,
    },
    {
      path: '/navigationPage',
      name: 'navigationPage',
      component: navigationPage,
      meta: {
        requiresAuth: true,// 说明需要鉴权
      }
    },
    {
      path: '/frame',
      name: 'frame',
      component: frame,
      meta: {
        requiresAuth: true,// 说明需要鉴权
      }
    },
    {path: '*', name: 'noFound', component: notFound}
  ]
})


// 全局守卫  https://router.vuejs.org/zh-cn/advanced/navigation-guards.html
router.beforeEach((to, from, next) => {
  // 获得那些路由需要token验证
  // console.log("to", to);
  // console.log("from", from);
  // console.log("next", next);
  let userData = JSON.parse(session.getItem('userData'));

  if (to.path == "/" && userData) {
      next('navigationPage');
  } else if (to.matched.some(route => route.meta.requiresAuth)) {
    if (userData) {
      next();
    } else {
      // 没有登录的提示
      Message.error('您还没有登录');
      next('/');
    }
  } else {
    next(); // 确保一定要调用
  }
  // next();
});

export default router;
